<template>
  <div>
    <el-upload
      v-if="!imgUrl"
      action="#"
      list-type="picture-card"
      :before-upload="beforeupload"
      :http-request="httprequest"
    >
      <i class="el-icon-plus"></i>
    </el-upload>

    <!-- 默认图片 -->
    <div v-else class="preview">
      <img @click="preview" v-errorimg="defaultImg" class="img" :src="imgUrl" alt="" />
      <i class="el-icon-close" @click="close"></i>
    </div>

    <!-- 进度条 -->
    <el-progress
      v-show="showProgress"
      :show-text="false"
      class="progress"
      :percentage="percentage"
    ></el-progress>

    <!-- 图片预览 -->
    <div class="box">
      <el-dialog :visible.sync="showpreview">
        <img class="myimg" :src="imgUrl" alt="" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'

export default {
  name: 'imgupload',
  props: ['imgUrl', 'dialogVisible'],
  data () {
    return {
      showpreview: false,
      cosObj: null,
      percentage: 0,
      showProgress: false,
         defaultImg: require('@/assets/common/bigUserHeader.png')
    }
  },
  methods: {
    //   预览
    preview () {
      this.showpreview = true
    },
    // 关闭
    close () {
      this.$emit('update:imgUrl', '')
    },
    // 上传前回调
    beforeupload (file) {
      const less = file.size < 2 * 1024 * 1024
      const isType = file.type === 'image/png' || file.type === 'image/jpeg'

      if (!less) {
        this.$message.error('文件大小不得大于2M')
      }

      if (!isType) {
        this.$message.error('文件格式必须是 png 或 jpeg')
      }

      return less && isType
    },
    // 自定义上传
    httprequest (file) {
      // console.log(file);
      this.showProgress = true
      const myfile = file.file
      this.cosObj.putObject(
        {
          Bucket: 'hr73-1307224077' /* 必须 */,
          Region: 'ap-nanjing' /* 存储桶所在地域，必须字段 */,
          Key: myfile.name /* 必须 */,
          StorageClass: 'STANDARD',
          Body: myfile, // 上传文件对象
          onProgress: progressData => {
            // progressData: 上传过程中的实时进度对象
            // console.log(JSON.stringify(progressData))
            this.percentage = progressData.percent * 100
            setTimeout(() => {
              if (progressData.percent == 1) {
                this.showProgress = false
              }
            }, 1000)
          }
        },
        (err, data) => {
          //   data: 上传成功后的返回值
          //   err: 上传出错的错误信息
          //   console.log(err || data)
          this.$emit('update:imgUrl', 'http://' + data.Location)
        }
      )
    }
  },
  created () {
    //   初始化腾讯云cos
    this.cosObj = new COS({
      SecretId: 'AKIDLWR4RYP0KOBhwugEVVEfpiLipfHcgTzF', // 身份识别 ID
      SecretKey: 'zi9JDqBG6fxxh9KDqCG3arqYy81cx2qM' // 身份密钥
    })
  }
}
</script>

<style lang="scss" scoped>
.preview {
  width: 150px;
  height: 150px;
  position: relative;
  .img {
    width: 100%;
    height: 100%;
  }

  i {
    position: absolute;
    right: 0;
    top: 0;
  }
}

.box {
  text-align: center;
  .myimg {
    width: 400px;
    height: 400px;
  }
}

.progress {
  margin-top: 5px;
  width: 150px;
}
</style>
